package cn.jxau.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;

import cn.jxau.bean.po.Article;
import cn.jxau.bean.po.ArticleType;
import cn.jxau.bean.po.User;
import cn.jxau.bean.po.UserCollections;
import cn.jxau.bean.po.UserCollectionsType;
import cn.jxau.bean.vo.JsonResult;
import cn.jxau.common.util.JacksonUtils;

@Controller
@RequestMapping("coll")
public class CollectionController extends BaseController{
	
	@RequestMapping("collList")
	@ResponseBody
	public JsonResult getCollectionByUserID(Page<Article> page,HttpServletRequest request){
		JsonResult result=null;
		List<Article> list = null;
		User user = (User) request.getSession().getAttribute("user");
		if(user!=null){
			list=collectionsService.getCollectionByUserID(page,user.getId());
		}
		if(list!=null&&list.size()>0){
			result=renderSuccess("获取列表成功!");
			result.addData("list", list);
		}else{
			result=renderError("学生列表为空或者发生服务器错误");
		}
		result.addData("page", page);
		return result;
	}
	
	
	@RequestMapping("usertype")
	@ResponseBody
	public JsonResult getAllArticle(HttpSession session){
		JsonResult result = renderSuccess("响应成功");
		List<UserCollectionsType> list = null;
		User user = (User) session.getAttribute("user");
		if(user!=null){
			list = collectionsService.getUserArticleType(user.getId());
		}
		result.addData("list", list);
		return result;
	}
	
	@RequestMapping("insert")
	@ResponseBody
	public JsonResult insertType(String userCollectionsType,HttpSession session){
		JsonResult result = renderSuccess("响应成功");
		UserCollectionsType type = JacksonUtils.converToBean(UserCollectionsType.class, userCollectionsType);
		User user = (User) session.getAttribute("user");
		if(user!=null){
			type.setUserID(user.getId());
			Long insertID = collectionsService.insertType(type);
			if(insertID>0){
				result.addData("reply", 1);
				result.addData("insertID", insertID);
			}else{
				result.addData("reply", 0);
			}
		}
		return result;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public JsonResult deleteById(Long id,boolean isParent,HttpSession session){
		JsonResult result = renderSuccess("响应成功");
		User user = (User) session.getAttribute("user");
		if(user!=null){
			if(collectionsService.deleteTypeParentById(id,isParent,user.getId())>0){
				result.addData("reply", 1);
			}else{
				result.addData("reply", 0);
			}
		}
		return result;
	}
	
	@RequestMapping("upname")
	@ResponseBody
	public JsonResult updateName(String userCollectionsType,HttpSession session){
		JsonResult result = renderSuccess("响应成功");
		User user = (User) session.getAttribute("user");
		if(user!=null){
			UserCollectionsType type = JacksonUtils.converToBean(UserCollectionsType.class, userCollectionsType);
			if(collectionsService.updateTypeNameById(type)>0){
				result.addData("reply", 1);
			}else{
				result.addData("reply", 0);
			}
		}
		return result;
	}
	
	@RequestMapping("deleteColl")
	@ResponseBody
	public JsonResult deleteCollection(Long articleID,HttpSession session){
		System.out.println(articleID);
		JsonResult result = renderSuccess("响应成功");
		User user = (User) session.getAttribute("user");
		if(user!=null){
			if(collectionsService.deleteCollectionByID(articleID,user.getId())>0){
				result.addData("reply", 1);
			}else{
				result.addData("reply", 0);
			}
		}
		return result;
	}
	
	@RequestMapping("insertColl")
	@ResponseBody
	public JsonResult insertCollection(Long articleID,Long usertypeID,HttpSession session){
		JsonResult result = renderSuccess("响应成功");
		User user = (User) session.getAttribute("user");
		if(user!=null){
			if(collectionsService.insertCollectionByID(articleID,usertypeID,user.getId())>0){
				result.addData("reply", 1);
			}else{
				result.addData("reply", 0);
			}
		}
		return result;
	}
	
	@RequestMapping("/typeid")
	@ResponseBody
	public JsonResult getUserArticleByTypeID(Page<Article> page,Long typeID,HttpSession session){
		JsonResult result=null;
		User user = (User) session.getAttribute("user");
		if(user!=null){
			List<Article> list=collectionsService.getUserArticleByTypeID(page,typeID,user.getId());
			if(list!=null&&list.size()>0){
				result=renderSuccess("获取列表成功!");
			}else{
				result=renderError("文章列表为空或者发生服务器错误");
			}
			result.addData("lists", list);
			result.addData("page", page);
		}
		
		return result;
	}
	
	@RequestMapping("/parentid")
	@ResponseBody
	public JsonResult getArticleByParentID(Page<Article> page,Long typeID,HttpSession session){
		JsonResult result=null;
		User user = (User) session.getAttribute("user");
		if(user!=null){
			List<Article> list=collectionsService.getUserArticleByParentID(page,typeID,user.getId());
			if(list!=null&&list.size()>0){
				result=renderSuccess("获取列表成功!");
			}else{
				result=renderError("文章列表为空或者发生服务器错误");
			}
			result.addData("lists", list);
			result.addData("page", page);
		}
		return result;
	}
}
